package com.sky.controller.user;

import com.sky.dto.PageQuery;
import com.sky.dto.ReviewDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.ReviewServiceImpl;
import com.sky.vo.ReviewResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "C端-书评的相关接口")
@Slf4j
@RequestMapping("/user/review")
public class ReviewController {
    @Autowired
    private ReviewServiceImpl reviewService;

    /**
     * 添加书评
     *
     * @param reviewDTO
     * @return
     */
    @PostMapping
    @ApiOperation("添加书评")
    public Result add(@RequestBody ReviewDTO reviewDTO) {
        log.info("新创建的书评：{}", reviewDTO);
        reviewService.add(reviewDTO);
        return Result.success();
    }

    /**
     * 根据id获取书评详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取书评详情")
    public Result<ReviewResult> detail(@PathVariable String id) {
        ReviewResult result = reviewService.getReviewResult(id);
        return Result.success(result);
    }

    /**
     * 分页查询已发布的书评
     *
     * @return
     */
    @Cacheable(value = "review", key = "#pageQuery.page + '_' + #pageQuery.size", unless = "#result == null")
    @GetMapping()
    @ApiOperation("分页查询已发布的书评")
    public Result<PageResult> list(PageQuery pageQuery) {
        PageResult result = reviewService.list(pageQuery);
        return Result.success(result);
    }

}